package com.mapapp.flutter_mapapp.activity;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.adapter.MyTaskAdapter;
import com.mapapp.flutter_mapapp.bean.MyTaskBean;
import com.mapapp.flutter_mapapp.bean.TaskItem;
import com.mapapp.flutter_mapapp.persenter.MyTaskPresenter;
import com.mapapp.flutter_mapapp.util.ToastUtils;
import com.mapapp.flutter_mapapp.view.MyTaskViewInterface;
import com.mapapp.flutter_mapapp.widget.TitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyTaskActivity extends ImmersiveBaseActivity implements MyTaskViewInterface {

    @BindView(R.id.rv_my_task)
    RecyclerView rvMyTask;
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    private MyTaskPresenter presenter;

    private MyTaskAdapter taskAdapter;


    private static final String[] ITEM_NAME_LIST = {"新任务", "正在进行中的任务", "已完成任务", "领取任务"};
    private static final int[] SRC_ID_LIST = {R.mipmap.icon_addtask, R.mipmap.icon_ingtask,
            R.mipmap.icon_finishtask, R.mipmap.icon_clatask};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_task);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        presenter = new MyTaskPresenter(this, this, lifecycleSubject);
        rvMyTask.setLayoutManager(new LinearLayoutManager(this));
        final List<MyTaskBean> list = new ArrayList<>();
        for (int i = 0; i < ITEM_NAME_LIST.length; i++) {
            MyTaskBean myTaskBean = new MyTaskBean();
            myTaskBean.setItemName(ITEM_NAME_LIST[i]);
            myTaskBean.setSrcId(SRC_ID_LIST[i]);
            myTaskBean.setCanClick(i != 3);
            list.add(myTaskBean);
        }

        taskAdapter = new MyTaskAdapter(R.layout.item_my_task, list);
        rvMyTask.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                if (parent.getChildLayoutPosition(view) == 0) {
                    outRect.top = SizeUtils.dp2px(8);
                }
            }
        });
        rvMyTask.setAdapter(taskAdapter);
        taskAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MyTaskBean myTaskBean = taskAdapter.getData().get(position);
                if (myTaskBean.getTaskItems() != null && myTaskBean.getTaskItems().size() > 0) {
                    if (myTaskBean.getTaskItems() != null) {
                        Intent intent = new Intent(mContext, NewTaskActivity.class);
                        intent.putExtra("taskItems", GsonUtils.toJson(myTaskBean.getTaskItems()));
                        intent.putExtra("isNew", position == 0);
                        startActivity(intent);
                    }
                }
                else
                {
                    ToastUtils.showMessage(mContext, "暂无数据");
                }
            }
        });

        titleBar.getIvBack().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.getTaskList();
    }

    @Override
    public void showTaskList(List<TaskItem> taskItems) {
        if (taskItems != null) {
            int itemCountNew = 0;
            int itemCountInProgress = 0;
            int itemCountFinished = 0;

            final List<TaskItem> listNew = new ArrayList<>();
            final List<TaskItem> listInProgress = new ArrayList<>();
            final List<TaskItem> listFinished = new ArrayList<>();

            for (TaskItem taskItem : taskItems) {
                if (taskItem != null) {
                    switch (taskItem.getState()) {
                        case 0: //新任务
                            itemCountNew++;
                            listNew.add(taskItem);
                            break;
                        case 1: //进行中
                            itemCountInProgress++;
                            listInProgress.add(taskItem);
                            break;
                        case 3:  //已完成
                            itemCountFinished++;
                            listFinished.add(taskItem);
                            break;
                    }
                }
            }
            for (MyTaskBean datum : taskAdapter.getData()) {
                if (datum.getItemName().equals(ITEM_NAME_LIST[0])) {
                    datum.setCount(itemCountNew);
                    datum.setTaskItems(listNew);
                } else if (datum.getItemName().equals(ITEM_NAME_LIST[1])) {
                    datum.setCount(itemCountInProgress);
                    datum.setTaskItems(listInProgress);
                } else if (datum.getItemName().equals(ITEM_NAME_LIST[2])) {
                    datum.setCount(itemCountFinished);
                    datum.setTaskItems(listFinished);
                }
            }

            taskAdapter.notifyDataSetChanged();
        }
        if (taskAdapter.getData().isEmpty()) {
            ToastUtils.showMessage(mContext, "暂无数据");
        }
    }
}
